package com.huak.view.template.engine;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author LR2SD
 * @description
 * @date 2019-04-21 10:49
 */
public class ThymeleafTemplateEngineBootstrap {


    public static void main(String [] args)throws IOException{
        //spring template engine
        SpringTemplateEngine springTemplateEngine=new SpringTemplateEngine();

        //内容上下文
        Context context=new Context();



        //绑定值
        context.setVariable("message","hello,thymeleaf");

        //读取内容 classpath:templates/thymeleaf/hello.html
        //模版
       // String template="<p th:text=\"${message}\">!!!</p>";

        ResourceLoader resourceLoader=new DefaultResourceLoader();
        //读取resource
        Resource resource=resourceLoader.getResource("classpath:templates/thymeleaf/hello.html");

        File file=resource.getFile();

        FileInputStream fileInputStream=new FileInputStream(file);

        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();



        IOUtils.copy(fileInputStream,byteArrayOutputStream);
        
        fileInputStream.close();
        String result=springTemplateEngine.process(byteArrayOutputStream.toString("utf-8"),context);


        //渲染
        //String result=springTemplateEngine.process(template,context);

        System.out.println(result);


    }

}
